import * as types from './actionTypes';
import {cache_set,cache_get} from '../../utils/browserCache';
import Properties from '../../../../config/properties.js';
import Request from '../../utils/request.js';
import { forwardTo } from '../../utils/index';

import API from '../../../../config/api';
import reqwest from 'reqwest';

export function loginSucess(){
    //debugger;
    return {type:types.LOGIN_SUCESS}
}
function _logout(){
    return {type:types.LOGOUT};
}
export function doLogout(){
    return (dispatch)=>{
        reqwest(Request.getOptions(
            "delete",
            API.logout,
            null,
            true
        )).then( response => {
            dispatch(_logout());
            Request.removeSession()
            location.href = "#/auth/login";
        }).fail(function (err) {
            Request.handleError(err);
        }).always((resp) =>{});
    }
}
function _inited(info){
    return {type:types.INITED,info};
}
export function showScrollToTopFloat(visible = false){
    return {type:types.SHOW_SCROLL_TO_TOP_FLOAT,visible};
}
export function scrolledBottom(isScrolledBottom){
    return {type:types.SCROLLED_BOTTOM,isScrolledBottom};
}
export function goTop(flag){
    return {type:types.GO_TOP,flag};
}

export function init(router){
    return function (dispatch, getState) {
        reqwest(Request.getOptions(
            "get",
            API.appInfo,
            null,
            true
        )).then( response => {
            dispatch(_inited(response));
            document.title = response.appConfig.title;
        }).fail(function (err) {
            Request.handleError(err);
        }).always((resp) =>{

        });
    };
}
